
import { queryAllAddress, exportFeedback } from './feedbackListServ.js'
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import { message } from 'antd';

const defultState = {
    loading: false,
    btnLoading: false,
    biz: {
        queryForm: {}
    },
    areaList: [],
    queryList: {}
}

// 声明module
const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    //   dispatch({type: 'queryInfo', payload: {}}) // end dispatch
                    dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
                    dispatch({ type: 'getAreaList', payload: {} });
                }
            })
        },
    },


    // 方法列表
    effects: {
        // 查询记录

        *getAreaList({ payload }, { put, call, select }) {
            try {
                const data = yield call(queryAllAddress, {})
                if (data.success) {
                    let areaList = []
                    //澳门与北京只有两级
                    let arr = [data.data[0]]
                    let attr = [data.data[data.data.length - 1]]
                    data.data[0] = {
                        label: "北京",
                        value: "110100",
                        children: arr
                    }
                    data.data[data.data.length - 1] = {
                        label: "澳门",
                        value: "820000",
                        children: attr
                    }
                    areaList = data.data

                    yield put({ type: 'updateStore', payload: { areaList } });
                }
            } catch (e) {

            }
        },
        *exportFeedback({ payload }, { put, call, select }) {
            let { queryList } = yield select(state => state[tmpModule.namespace])
            console.log(queryList)
            let params = { pageSize: 9999, pageNo: 1, ...queryList }
            let { data, success } = yield call(exportFeedback, params)
            if (success) {
                //console.log(data)
                message.success('导出成功,正在为您下载表格')
                window.location.href = data
            }
        }
    },

    // reducers: {
    //   updateStore(preState, action) {
    //     return Object.assign({}, preState, action.payload)
    //   },
    // }
    reducers: {
        updateStore(preState, action) {
            return { ...preState, ...action.payload };
        }
    }
}

export default tmpModule
